// Fill out your copyright notice in the Description page of Project Settings.


#include <UI/Widget/SSlateDemoOtherWidget.h>
#include "SlateOptMacros.h"
#include "Helper/CustomGameinstance.h"
#include "Widgets/Layout/SScrollBox.h"
#include "Widgets/Layout/SGridPanel.h"
#include "Widgets/Images/SImage.h"
#include "Widgets/Layout/SConstraintCanvas.h"
#include "Async/Async.h"
#include "UI/Style/SlateDemoManager.h"
#include "UI/Style/SlateDemoWidgetStyle.h"
#include "JsonUtilities/Public/JsonObjectConverter.h"
#include <Policies/CondensedJsonPrintPolicy.h>

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SSlateDemoOtherWidget::Construct(const FArguments& InArgs)
{
	const struct FSlateDemoStyle* ToolbarStyle = &SlateDemoManager::Get().GetWidgetStyle<FSlateDemoStyle>(
		"SlateDemoStyle_BP");
	FString a = FPaths::SourceConfigDir();
	GetImage = NewObject<UCustomGameinstance>();
	ChildSlot
	[
		SNew(SScrollBox)
		+ SScrollBox::Slot()
		.Padding(5.0f)
		[
			SNew(SGridPanel)
			.FillColumn(0, 0.1f)
			.FillColumn(1, 0.9f)
#pragma region 获取本地图片
			+ SGridPanel::Slot(0, 0)
			[
				SNew(STextBlock)
				.Text(FText::FromString(TEXT("获取本地图片")))
				.ToolTipText(FText::FromString(TEXT("获取本地图片")))
			]
			+ SGridPanel::Slot(1, 0)
			[
				SNew(SConstraintCanvas)
				+ SConstraintCanvas::Slot()
				  .Anchors(FAnchors(0.0, 0.0, 0.0, 0.0)) //锚点,这里需要设置为0
				  .Alignment(FVector2D(0.0, 0.0)) //轴(设置为0,0则代表控件的坐上角坐标为起始原点,如果设置为1,1 则代表控件的右下角为其实原点)
				  .Offset(FMargin(0, 0, 128, 128))
				[
					SAssignNew(LocalImage, SImage)
				]

			]
			+ SGridPanel::Slot(0, 1)
			[
				SNew(STextBlock)
					.Text(FText::FromString(TEXT("获取资源图片")))
					.ToolTipText(FText::FromString(TEXT("获取资源图片")))
			]
			+ SGridPanel::Slot(1, 1)
			[
				SNew(SConstraintCanvas)

				+ SConstraintCanvas::Slot()
				  .Anchors(FAnchors(0.0, 0.0, 0.0, 0.0)) //锚点,这里需要设置为0
				  .Alignment(FVector2D(0.0, 0.0)) //轴(设置为0,0则代表控件的坐上角坐标为起始原点,如果设置为1,1 则代表控件的右下角为其实原点)
				  .Offset(FMargin(0, 0, 128, 128))
				[
					SAssignNew(AssetImage, SImage)
				]

			]
			+ SGridPanel::Slot(0, 2)
			[
				SNew(STextBlock)
					.Text(FText::FromString(TEXT("多线程测试")))
					.ToolTipText(FText::FromString(TEXT("多线程测试")))
			]
			+ SGridPanel::Slot(1, 2)
			[
				SNew(SButton).OnClicked(this, &SSlateDemoOtherWidget::ThreadingDemo)


			]
			+ SGridPanel::Slot(0, 3)
			[
				SNew(STextBlock)
					.Text(FText::FromString(TEXT("JSON序列化与反序列化")))
				.ToolTipText(FText::FromString(TEXT("")))
			]
			+ SGridPanel::Slot(1, 3)
			[
				SNew(SButton).OnClicked(this, &SSlateDemoOtherWidget::JsonToStructAndStructToJson)


			]
			+ SGridPanel::Slot(0, 4)
			[
				SNew(STextBlock)
				.Text(FText::FromString(TEXT("图片九宫格")))
			]
			+ SGridPanel::Slot(1, 4)
			[
				SNew(SHorizontalBox)
				+ SHorizontalBox::Slot().AutoWidth().Padding(0, 0, 20, 0)
				[
					SNew(SImage).Image(&ToolbarStyle->NineEnable)
				]
				+ SHorizontalBox::Slot().FillWidth(1).Padding(0, 0, 20, 0)
				[
					SNew(SImage).Image(&ToolbarStyle->NineEnable)
				]
				+ SHorizontalBox::Slot().FillWidth(1).Padding(0, 0, 20, 0)
				[
					SNew(SImage).Image(&ToolbarStyle->NineDesable)
				]
			]

#pragma endregion
		]
	];
	FString ContentDir = FPaths::ProjectContentDir();
	ContentDir.Append(TEXT("UI/img/666.uasset"));
	if (GetImage->GetAssetPNGToUTexture2D(ContentDir, AssetBrush)) //从资源文件中获取缩略图
	{
		AssetImage->SetImage(&AssetBrush);
	}
	ContentDir = FPaths::ProjectContentDir();
	ContentDir.Append("UI/img/1.jpg");
	if (GetImage->GetImageToBrush(ContentDir, LocalBrush))
	{
		LocalImage->SetImage(&LocalBrush);
	}
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION

bool Simulationfun(int c)
{
	int ileng = rand() % 9999;
	int jleng = rand() % 999999;
	int64 count = 0;
	for (int i = 0; i < ileng * c; i++)
		for (int j = 0; j < jleng; j++)
			count++;
	return true;
}

FReply SSlateDemoOtherWidget::ThreadingDemo()
{
	TAtomic<uint32> TasksDone(0);
	TArray<TFuture<bool>> TasksIsMeshValidResult;
	//执行多线程
	for (int i = 0; i < 100; i++)
	{
		TasksIsMeshValidResult.Add(
			Async(
				EAsyncExecution::LargeThreadPool,
				[i]() { return Simulationfun(i); },
				[&TasksDone]() { TasksDone++; }
			)
		);
	}

	//等待所有的线程执行完毕
	for (int32 OldTasksDone = 0, NewTasksDone = 0; OldTasksDone != 100; OldTasksDone = NewTasksDone)
	{
		NewTasksDone = TasksDone.Load();
		UE_LOG(LogTemp, Log, TEXT(" %d/100  loading... ..."), NewTasksDone);
		FPlatformProcess::Sleep(0.01);
	}
	return FReply::Handled();
}

FReply SSlateDemoOtherWidget::JsonToStructAndStructToJson()
{
	FTestStructOne teststruct;
	FString json = TEXT("\
	{\
		\"num\"  : 1,\
		\"flag\" : true,\
		\"str\"  : \"hello world\",\
		\"StructTwo\" : {\
		       \"num\"  : 2,\
		       \"flag\" : true\
		},\
		\"lasStructTwoArr\" : \
                [{\
			\"num\": 3,\
			\"flag\" : true\
		 },\
                 {\
			\"num\": 4,\
			\"flag\" : true\
		}]\
	}\
	");
	//序列化操作
	FJsonObjectConverter::JsonObjectStringToUStruct(json, &teststruct, 0, 0);

	FString StructToJson = "";
	//反序列化操作
	FJsonObjectConverter::UStructToFormattedJsonObjectString<TCHAR, TCondensedJsonPrintPolicy>(
		FTestStructOne::StaticStruct(), &teststruct, StructToJson, 0, 0);

	return FReply::Handled();
}
